package zhongbo.java.air.dictionary.web.controller;

import cn.hutool.core.date.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.dictionary.exception.BusinessException;
import zhongbo.java.air.dictionary.exception.ExceptionCode;
import zhongbo.java.air.dictionary.model.AirAlertZone;
import zhongbo.java.air.dictionary.model.enums.AlertZoneStatus;
import zhongbo.java.air.dictionary.service.IAirAlertZoneService;
import zhongbo.java.air.dictionary.vo.PageVo;
import zhongbo.java.air.dictionary.vo.RestResultVo;

@RestController
@RequestMapping("/airalertzone")
@Api(tags = "警戒区基础信息操作的相关接口")
public class AirAlertZoneController {
    @Autowired
    IAirAlertZoneService airAlertZoneService;

    @PostMapping("add")
    @ApiOperation("新增警戒区")
    RestResultVo<AirAlertZone> add(AirAlertZone airAlertZone){
        RestResultVo<AirAlertZone> airAlertZoneRestResultVo = CheckData(airAlertZone);
        AirAlertZone data = airAlertZoneRestResultVo.getData();
        if (data == null || data.equals("")){
            String massage = airAlertZoneRestResultVo.getMassage();
            return RestResultVo.failed(massage);
        }
        airAlertZone.setCreateTime(DateTime.now());
        airAlertZone.setZoneType(AlertZoneStatus.NO_FLY);
        int addAir = airAlertZoneService.addAir(airAlertZone);
        if (addAir <= 0){
           return RestResultVo.failed("添加失败");
        }
        return RestResultVo.ok(airAlertZone);
    }

    private RestResultVo<AirAlertZone> CheckData(AirAlertZone airAlertZone) {
        if (airAlertZone== null || airAlertZone.equals("")){
            return RestResultVo.failed("信息为空");
        }
        String zoneCode = airAlertZone.getZoneCode();
        String vertices = airAlertZone.getVertices();
        if (!zoneCode.matches("^[A-Z]+_(NOFLY|RESTRICTED|TEMPORARY)_\\d{0,4}$")){
            return RestResultVo.failed("区域业务编号不规范(例:BJ_NOFLY_001)");
        }
        if (!vertices.matches("\\\"lat\\\":\\d{1,}\\.\\d{0,2},\\\"lng\\\":\\d{1,}\\.\\d{0,2}")){
            return RestResultVo.failed("顶点坐标格式不规范(例:“lat”:39.9,“lng”:116.3})");
        }
        return RestResultVo.ok(airAlertZone);
    }

    @GetMapping("delete")
    @ApiOperation("删除信息")
    RestResultVo<AirAlertZone> delete(String id){
        if (id == null || id.equals("")) {
            RestResultVo.failed("id为空");
        }
        AirAlertZone airAlertZone = airAlertZoneService.selectById(id);
        airAlertZoneService.delete(id);
        return RestResultVo.success("删除成功! 原数据:" + airAlertZone);
    }

    @PostMapping("update")
    @ApiOperation("更新信息")
    RestResultVo<AirAlertZone> update(AirAlertZone airAlertZone){
        if (airAlertZone== null || airAlertZone.equals("")){
           return RestResultVo.failed("数据信息为空");
        }
        airAlertZone.setUpdateTime(DateTime.now());
        int update = airAlertZoneService.addAir(airAlertZone);
        if (update <= 0){
            return RestResultVo.failed("更新失败");
        }
        return RestResultVo.success("更新成功");
    }

    @GetMapping("datil")
    @ApiOperation("警戒区详细信息")
    RestResultVo<AirAlertZone> selectById(String id){
        if (id == null || id.equals("")){
            throw new BusinessException(ExceptionCode.ID_NULL);
        }
        AirAlertZone airAlertZone = airAlertZoneService.selectById(id);
        if (airAlertZone == null || airAlertZone.equals("")){
            return RestResultVo.failed("查询失败！");
        }
        return RestResultVo.ok(airAlertZone);
    }

    @GetMapping("page")
    @ApiOperation("分页查询")
    public RestResultVo<PageVo<AirAlertZone>> page(String zoneCode, Integer pageNumber, Integer pageSize){
        PageVo<AirAlertZone> pageVo = airAlertZoneService.page(zoneCode,pageNumber,pageSize);
        return RestResultVo.ok(pageVo);
    }
}
